﻿using System;
using System.Threading;

namespace Examples
{
    delegate void WorkDoneEventHandler(object sender, WorkDoneEventArgs e);
    class WorkDoneEventArgs
    {
        public ResultObject Result { get; set; }
    }


    class Example
    {
        #region methods
        public ResultObject DoWork(
            string stringParam)
        {
            return _doWork(stringParam);
        }

        public void DoWorkAsync(
            string stringParam)
        {
            Thread thrDoWork = new Thread(_doWorkAsync);
            thrDoWork.Start(stringParam);
        }
        #endregion
        #region privates
        private ResultObject _doWork(
            string stringParam)
        {
            //prepare result
            ResultObject ro = new ResultObject();

            //do something with stringParam
            if (string.IsNullOrEmpty(stringParam))
            {
                throw new ArgumentException("stringParam is bollocks", "stringParam");
            }

            //return result
            return ro;
        }

        private void _doWorkAsync(
            object stringParam)
        {
            //cast param to needed type
            string strStringParam = stringParam.ToString();

            //execute non async function
            ResultObject ro =_doWork(strStringParam);

            //throw event
            OnWorkDone(new WorkDoneEventArgs() { Result = ro });
        }
        #endregion
        #region events
        public event WorkDoneEventHandler WorkDone;
        protected void OnWorkDone(
            WorkDoneEventArgs ea)
        {
            if (WorkDone != null)
            {
                WorkDone(this, ea);
            }
        }
        #endregion
    }



    class ResultObject
    {
        object data { get; set; }
    }
}
